package com.zhangyue.programmer.servlet;

import com.google.gson.Gson;
import com.zhangyue.programmer.bean.User;
import com.zhangyue.programmer.common.BaseOperate;
import com.zhangyue.programmer.common.BasePage;
import com.zhangyue.programmer.common.R;
import com.zhangyue.programmer.dao.UserDao;
import com.zhangyue.programmer.dto.UserDto;
import com.zhangyue.programmer.util.Paged;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;

@WebServlet(name = "UserServlet",urlPatterns = "/UserServlet")
public class UserServlet extends HttpServlet {
    Gson gson = new Gson();
    UserDao dao = new UserDao();

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws javax.servlet.ServletException, IOException {
        response.setCharacterEncoding("utf-8");
        response.setContentType("application/json;charset=UTF-8");
        request.setCharacterEncoding("utf-8");
        String param = request.getParameter("param");
        UserDto userDto = gson.fromJson(param, UserDto.class);
        try {
            parseDto(response.getWriter(),userDto);
        } catch (Exception e) {
            e.printStackTrace();
            printObj(response.getWriter(),toJson(R.error("服务器异常")));
        }
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setCharacterEncoding("utf-8");
        request.setCharacterEncoding("utf-8");
        response.setContentType("application/json;charset=UTF-8");
        BufferedReader reader = request.getReader();
        UserDto userDto = gson.fromJson(reader, UserDto.class);
        try {
            parseDto(response.getWriter(),userDto);
        } catch (Exception e) {
            e.printStackTrace();
            printObj(response.getWriter(),toJson(R.error("服务器异常")));
        }

    }

    private void printObj(PrintWriter out, String str) {
        out.print(str);
        out.flush();
        out.close();
    }
    private <T> String toJson(T t) {
        R<T> result = R.ok(t);
        return gson.toJson(result);
    }

    private void parseDto(PrintWriter out, UserDto dto) throws Exception {
        String op = dto.getOp();
        if("add".equals(op)){
            add(out,dto);
        }else if("delete".equals(op)){
            delete(out,dto);
        }else if("update".equals(op)){
            update(out,dto);
        }else if("getAll".equals(op)){
            getAll(out,dto);
        }else if("getById".equals(op)){
            getById(out,dto);
        }
    }

    private void getById(PrintWriter out, UserDto dto) throws Exception {
        printObj(out,toJson(dao.getUserById(dto.getId())));
    }

    private void getAll(PrintWriter out, UserDto dto) throws Exception {
        printObj(out,toJson(dao.getUserAllList()));
    }

    private void update(PrintWriter out, UserDto dto) throws SQLException {
        printObj(out,toJson(dao.update(dto)));
    }

    private void delete(PrintWriter out, UserDto dto) throws SQLException {
        printObj(out,toJson(dao.delete(dto)));
    }


    private void add(PrintWriter out, UserDto dto) throws SQLException {
        printObj(out,toJson(dao.add(dto)));
    }

    public void queryByPage(PrintWriter out,BasePage page) throws Exception {
        int totalSize = dao.getTotalSize();
        List<User> list = dao.getUserByPaged(page.getCurrentPage(), page.getSize());
        Paged<User> paged = Paged.paging(page.getCurrentPage(), page.getSize(), totalSize, list);
        printObj(out,toJson(paged));
    }

}
